<?php
 
namespace app\common\job;
 
use Yii;
use yii\base\BaseObject;
use app\helpers\DatetimeHelper;
use yii\helpers\ArrayHelper;
use app\modules\notify\service\NotifyService;
use app\modules\notify\service\NotifyRangeService;
use app\modules\notify\service\NotifyLatestService;
use app\modules\notify\service\NotifyRangeLatestService;
use app\modules\notify\service\RemindInfoService;
use app\modules\inspect\service\InspectInfoService;
// use app\common\job\WxOfficialUnreadNotifyJob;
use app\modules\wxofficial\service\WxOfficialService;

use \yii\queue\Job;
 
class NotifyCommonAddJob extends BaseObject implements Job
{
    public $inputs;
    public $notifyId;
    public $userId;
    public $createTimestamp;
    public $rangeCount;

    public function execute($queue){
        $inputs = $this->inputs;
        $notifyId = $this->notifyId;
        $createTimestamp = $this->createTimestamp;
        $userId = $this->userId;
        $rangeCount = $this->rangeCount;
		
        //添加通知范围
        $rangeList = [];
        foreach($inputs['range_ids'] as $key => $rangeUserId)
        {
            $rangeList[$key]['status'] = 1;
            $rangeList[$key]['is_read'] = 0;
            $rangeList[$key]['notify_id'] = $notifyId;
            $rangeList[$key]['userid'] = $rangeUserId;
            $rangeList[$key]['create_timestamp'] = $createTimestamp;
            $rangeList[$key]['update_timestamp'] = $createTimestamp;
        }
        $rangeIds = (new NotifyRangeService)->saveMany($rangeList, ['notify_id', 'userid']);
        // $rangeNum = count($rangeIds);
        // $this->update(['unread_num' => $rangeNum],['id' => $notifyId]);
        
        // 添加到latest
        if($inputs['temporary'] == 0) {
            (new NotifyRangeLatestService)->addMany($rangeList);
        }
        
        // 添加督办信息
        $inspectAddData = [];
        $i = 0;
        foreach($inputs['inspectList'] ?? [] as $inspectInfo)
        {
            foreach($inspectInfo['inspectUserList'] as $inspectUserInfo)
            {
                if(!empty($inspectUserInfo)) {
                    $inspectUnitInfo = $inspectInfo['unitInfo'];
                    $inspectRangeIdList = ArrayHelper::getColumn($inspectInfo['rangeUserIdList'], 'user_id');
                    
                    $inspectAddData[$i]['inspect_user_id'] = (string)$inspectUserInfo['user_id'];
                    $inspectAddData[$i]['publish_user_id'] = (string)$userId;
                    $inspectAddData[$i]['unit_id'] = $inspectUnitInfo['id'];
                    $inspectAddData[$i]['range_id_json'] = json_encode($inspectRangeIdList);
                    $inspectAddData[$i]['type'] = 1; // 1为通知 2为征集
                    $inspectAddData[$i]['relate_id'] = $notifyId;
                    $inspectAddData[$i]['inspect_type'] = $inputs['inspectType'];
                    $inspectAddData[$i]['create_timestamp'] = $createTimestamp;
                    
                    $i ++;
                }
            }
        }
        (new InspectInfoService)->addMany($inspectAddData);
        
        
        // 必读通知 存储提醒事件
        $addRemind = [];
        foreach($inputs['remind_times'] as $key =>$r_time){
            $addRemind[$key]['publisher_id']=$userId;
            $addRemind[$key]['type']=1;
            $addRemind[$key]['relate_id']=$notifyId;
            $addRemind[$key]['timestamp']= $r_time;
            $addRemind[$key]['has_send']=0;   
            $addRemind[$key]['content']=($inputs['remind_contents'][$key] == '') ? $inputs['title'] : $inputs['remind_contents'][$key];
            $addRemind[$key]['is_remind_me']=$inputs['is_remind_me'][$key];
            $addRemind[$key]['is_remind_ontime']=$inputs['is_remind_ontime'][$key];
        }
        (new RemindInfoService)->addMany($addRemind);


        //完成所有工作，修改进度
        (new NotifyService)->update(['process'=>2],['id' => $notifyId]);    //阅读中
        if($inputs['temporary'] == 0) { // 修改latest
            (new NotifyLatestService)->update(['process'=>2],['id' => $notifyId]);
        }
		
		if($inputs['temporary'] == 0) {
			(new WxOfficialService)->notifyUnread($notifyId, $inputs['range_ids'], $userId);
		}
    }
}